<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/9
 * Time: 下午5:31
 */

namespace App\Http\Controllers;


use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\UserFilterServices;
use App\Http\Services\UserVerifyServices;
use App\Lib\faceTencent;
use Illuminate\Http\Request;
use App\Lib\Response;

class UserVerifyController extends Controller
{
    private $services_user_verify;
    public function __construct(UserVerifyServices $userVerifyServices)
    {
        $this->services_user_verify = $userVerifyServices;
    }

    public function getToken(Request $request){
        $idcardName = $request->get('idcard_name');
        $idcardNumber = $request->get('idcard_number');

        if (empty($idcardName)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if (empty($idcardNumber)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $idcardNumber = strtolower($idcardNumber);

        //add by zxiaofee 检查是否在禁止实名的身份证列表里
        if (UserFilterServices::inForbidVerifyIdCardList($idcardNumber)) {
            Response::returnResult(Hint::ERR_SYS_FAIL, null, '身份信息异常，禁止实名');
        }

        $token = $this->services_user_verify->getToken($idcardName, $idcardNumber);
        if(is_numeric($token)) {
            Response::returnResult($token);
        }

        Response::returnResult(Hint::SUCCESS, $token);
    }

    public function getSign(Request $request){
        $idcardName = $request->post('idcard_name');
        $idcardNumber = $request->post('idcard_number');

        if (empty($idcardName)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if (empty($idcardNumber)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $idcardNumber = strtolower($idcardNumber);

        //add by zxiaofee 检查是否在禁止实名的身份证列表里
        if (UserFilterServices::inForbidVerifyIdCardList($idcardNumber)) {
            Response::returnResult(Hint::ERR_SYS_FAIL, null, '身份信息异常，禁止实名');
        }

        $sign = $this->services_user_verify->getFaceSign($idcardName, $idcardNumber);
        if(is_numeric($sign)) {
            Response::returnResult($sign);
        }

        Response::returnResult(Hint::SUCCESS, $sign);
    }

    public function reverifyResult(Request $request){
        $sign = $request->post('sign');
        $isSuccess = $request->post('isSuccess');

        if (empty($sign)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $res = $this->services_user_verify->reverifyResult($sign);

        if (($isSuccess && $res == Hint::SUCCESS) || (!$isSuccess && $res != Hint::SUCCESS)) {
            Response::returnResult(Hint::SUCCESS);
        }

        Response::returnResult($res);
    }


         /*
         * 获取已经验证次数
         * @param [token]   用户token
         * */
    public function getVerifyCount(Request $request)
    {
        $count['num'] =  $this->services_user_verify->getUserCount();
        Response::returnResult(Hint::SUCCESS, $count);

    }
    public function complete(Request $request){
        $token = $request->post('token');
        $data = $request->file('meglive_data');
        if (empty($token)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if (empty($data)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $verify = $this->services_user_verify->complete($token, $data->getRealPath());
        if ($verify == Hint::ERROR){
            Response::returnResult(Hint::ERROR);
        }
        if ($verify == Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($token == Hint::USER_VERIFY_BEYOND){
            Response::returnResult(Hint::USER_VERIFY_BEYOND);
        }
        if ($verify == Hint::USER_VERIFY_NOT_FIND){
            Response::returnResult(Hint::USER_VERIFY_NOT_FIND);
        }
        if ($verify == Hint::USER_VERIFY_FAILED){
            Response::returnResult(Hint::USER_VERIFY_FAILED);
        }
        if ($verify == Hint::USER_VERIFY_PASS_LIVING_NOT_THE_SAME){
            Response::returnResult(Hint::USER_VERIFY_PASS_LIVING_NOT_THE_SAME);
        }
        if ($verify == Hint::USER_VERIFY_NO_ID_CARD_NUMBER){
            Response::returnResult(Hint::USER_VERIFY_NO_ID_CARD_NUMBER);
        }
        if ($verify == Hint::USER_VERIFY_ID_NUMBER_NAME_NOT_MATCH){
            Response::returnResult(Hint::USER_VERIFY_ID_NUMBER_NAME_NOT_MATCH);
        }
        if ($verify == Hint::USER_VERIFY_NO_FACE_FOUND){
            Response::returnResult(Hint::USER_VERIFY_NO_FACE_FOUND);
        }
        if ($verify == Hint::USER_VERIFY_NO_ID_PHOTO){
            Response::returnResult(Hint::USER_VERIFY_NO_ID_PHOTO);
        }
        if ($verify == Hint::USER_VERIFY_PHOTO_FORMAT_ERROR){
            Response::returnResult(Hint::USER_VERIFY_PHOTO_FORMAT_ERROR);
        }
        if ($verify == Hint::USER_VERIFY_DATA_SOURCE_ERROR){
            Response::returnResult(Hint::USER_VERIFY_DATA_SOURCE_ERROR);
        }
        if ($verify == Hint::USER_VERIFY_FAIL_LIVING_FACE_ATTACK){
            Response::returnResult(Hint::USER_VERIFY_FAIL_LIVING_FACE_ATTACK);
        }
        if ($verify == Hint::USER_VERIFY_VIDEO_LACK_FRAMES){
            Response::returnResult(Hint::USER_VERIFY_VIDEO_LACK_FRAMES);
        }
        if ($verify == Hint::USER_VERIFY_FAIL_EYES_CLOSE_DETECTION){
            Response::returnResult(Hint::USER_VERIFY_FAIL_EYES_CLOSE_DETECTION);
        }
        if ($verify == Hint::USER_VERIFY_BIZ_TOKEN_DENIED){
            Response::returnResult(Hint::USER_VERIFY_BIZ_TOKEN_DENIED);
        }
        if ($verify == Hint::USER_VERIFY_AUTHENTICATION_FAIL){
            Response::returnResult(Hint::USER_VERIFY_AUTHENTICATION_FAIL);
        }
        if ($verify == Hint::USER_VERIFY_MOBILE_PHONE_NOT_SUPPORT){
            Response::returnResult(Hint::USER_VERIFY_MOBILE_PHONE_NOT_SUPPORT);
        }
        if ($verify == Hint::USER_VERIFY_SDK_TOO_OLD){
            Response::returnResult(Hint::USER_VERIFY_SDK_TOO_OLD);
        }
        if ($verify == Hint::USER_VERIFY_MOBILE_PHONE_NO_AUTHORITY){
            Response::returnResult(Hint::USER_VERIFY_MOBILE_PHONE_NO_AUTHORITY);
        }
        if ($verify == Hint::USER_VERIFY_USER_CANCELLATION){
            Response::returnResult(Hint::USER_VERIFY_USER_CANCELLATION);
        }
        if ($verify == Hint::USER_VERIFY_USER_TIMEOUT){
            Response::returnResult(Hint::USER_VERIFY_USER_TIMEOUT);
        }
        if ($verify == Hint::USER_VERIFY_VERIFICATION_FAILURE){
            Response::returnResult(Hint::USER_VERIFY_VERIFICATION_FAILURE);
        }
        if ($verify == Hint::USER_VERIFY_UNDETECTED_FACE){
            Response::returnResult(Hint::USER_VERIFY_UNDETECTED_FACE);
        }
        if ($verify == Hint::USER_VERIFY_ACTION_ERROR){
            Response::returnResult(Hint::USER_VERIFY_ACTION_ERROR);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    public function getReverifyToken(Request $request){
        $idcardName = $request->get('idcard_name');
        $idcardNumber = $request->get('idcard_number');

        if (empty($idcardName)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if (empty($idcardNumber)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $idcardNumber = strtolower($idcardNumber);

        $token = $this->services_user_verify->getReveiryToken($idcardName, $idcardNumber);
        //未登录
        if ($token == Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        //身份证姓名不匹配
        if ($token == Hint::USER_VERIFY_ID_NUMBER_NAME_NOT_MATCH){
            Response::returnResult(Hint::USER_VERIFY_ID_NUMBER_NAME_NOT_MATCH);
        }
        Response::returnResult(Hint::SUCCESS, $token);
    }

    public function reverifyComplete(Request $request){
        $token = $request->post('token');
        $data = $request->file('meglive_data');
        if (empty($token)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if (empty($data)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $verify = $this->services_user_verify->reverifyComplete($token, $data->getRealPath());
        if ($verify == Hint::ERROR){
            Response::returnResult(Hint::ERROR);
        }
        if ($verify == Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($token == Hint::USER_VERIFY_BEYOND){
            Response::returnResult(Hint::USER_VERIFY_BEYOND);
        }
        if ($verify == Hint::USER_VERIFY_NOT_FIND){
            Response::returnResult(Hint::USER_VERIFY_NOT_FIND);
        }
        if ($verify == Hint::USER_VERIFY_FAILED){
            Response::returnResult(Hint::USER_VERIFY_FAILED);
        }
        if ($verify == Hint::USER_VERIFY_PASS_LIVING_NOT_THE_SAME){
            Response::returnResult(Hint::USER_VERIFY_PASS_LIVING_NOT_THE_SAME);
        }
        if ($verify == Hint::USER_VERIFY_NO_ID_CARD_NUMBER){
            Response::returnResult(Hint::USER_VERIFY_NO_ID_CARD_NUMBER);
        }
        if ($verify == Hint::USER_VERIFY_ID_NUMBER_NAME_NOT_MATCH){
            Response::returnResult(Hint::USER_VERIFY_ID_NUMBER_NAME_NOT_MATCH);
        }
        if ($verify == Hint::USER_VERIFY_NO_FACE_FOUND){
            Response::returnResult(Hint::USER_VERIFY_NO_FACE_FOUND);
        }
        if ($verify == Hint::USER_VERIFY_NO_ID_PHOTO){
            Response::returnResult(Hint::USER_VERIFY_NO_ID_PHOTO);
        }
        if ($verify == Hint::USER_VERIFY_PHOTO_FORMAT_ERROR){
            Response::returnResult(Hint::USER_VERIFY_PHOTO_FORMAT_ERROR);
        }
        if ($verify == Hint::USER_VERIFY_DATA_SOURCE_ERROR){
            Response::returnResult(Hint::USER_VERIFY_DATA_SOURCE_ERROR);
        }
        if ($verify == Hint::USER_VERIFY_FAIL_LIVING_FACE_ATTACK){
            Response::returnResult(Hint::USER_VERIFY_FAIL_LIVING_FACE_ATTACK);
        }
        if ($verify == Hint::USER_VERIFY_VIDEO_LACK_FRAMES){
            Response::returnResult(Hint::USER_VERIFY_VIDEO_LACK_FRAMES);
        }
        if ($verify == Hint::USER_VERIFY_FAIL_EYES_CLOSE_DETECTION){
            Response::returnResult(Hint::USER_VERIFY_FAIL_EYES_CLOSE_DETECTION);
        }
        if ($verify == Hint::USER_VERIFY_BIZ_TOKEN_DENIED){
            Response::returnResult(Hint::USER_VERIFY_BIZ_TOKEN_DENIED);
        }
        if ($verify == Hint::USER_VERIFY_AUTHENTICATION_FAIL){
            Response::returnResult(Hint::USER_VERIFY_AUTHENTICATION_FAIL);
        }
        if ($verify == Hint::USER_VERIFY_MOBILE_PHONE_NOT_SUPPORT){
            Response::returnResult(Hint::USER_VERIFY_MOBILE_PHONE_NOT_SUPPORT);
        }
        if ($verify == Hint::USER_VERIFY_SDK_TOO_OLD){
            Response::returnResult(Hint::USER_VERIFY_SDK_TOO_OLD);
        }
        if ($verify == Hint::USER_VERIFY_MOBILE_PHONE_NO_AUTHORITY){
            Response::returnResult(Hint::USER_VERIFY_MOBILE_PHONE_NO_AUTHORITY);
        }
        if ($verify == Hint::USER_VERIFY_USER_CANCELLATION){
            Response::returnResult(Hint::USER_VERIFY_USER_CANCELLATION);
        }
        if ($verify == Hint::USER_VERIFY_USER_TIMEOUT){
            Response::returnResult(Hint::USER_VERIFY_USER_TIMEOUT);
        }
        if ($verify == Hint::USER_VERIFY_VERIFICATION_FAILURE){
            Response::returnResult(Hint::USER_VERIFY_VERIFICATION_FAILURE);
        }
        if ($verify == Hint::USER_VERIFY_UNDETECTED_FACE){
            Response::returnResult(Hint::USER_VERIFY_UNDETECTED_FACE);
        }
        if ($verify == Hint::USER_VERIFY_ACTION_ERROR){
            Response::returnResult(Hint::USER_VERIFY_ACTION_ERROR);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    public function getInfo(Request $request){
        $data = $this->services_user_verify->getInfo();
        if ($data == Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data == Hint::USER_VERIFY_NOT_ACTION){
            Response::returnResult(Hint::USER_VERIFY_NOT_ACTION);
        }
        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * wap session 获取用户信息
     * */
    public function getInfoByUser(Request $request){

        $params = $request->post();
        $rest = $this->services_user_verify->getInfoByUser($params);
        Response::returnResult($rest['code'],$rest['data']);
    }

    /**
     * cms 添加用户验证
     * */
    public function createUserVerify(Request $request)
    {
        $user_id = $request->post('user_id');
        $idcardName = $request->post('idcardName');
        $idNumber = $request->post('idcard');
        if (empty($idcardName)) {
            Response::returnResult(Hint::PARAM_ERROR, []);
        }
        if (empty($idNumber)) {
            Response::returnResult(Hint::PARAM_ERROR, []);
        }
        if (empty($user_id)) {
            Response::returnResult(Hint::PARAM_ERROR, []);
        }
        $idNumber = strtolower($idNumber);
        $hint =  $this->services_user_verify->create_user_verify($user_id,$idcardName,$idNumber);
        Response::returnResult($hint, []);
    }

    public function clearVerify(Request $request)
    {
        $user_id = $request->post('user_id');
        if (!$user_id){
            Response::returnResult(Hint::PARAM_ERROR, [], 'user_id 必填');
        }
        $data = $this->services_user_verify->clearVerify($user_id);
        if (!$data){
            Response::returnResult(Hint::ERROR);
        }
        Response::returnResult(Hint::SUCCESS);
    }

}
